<?php
namespace app\appoint\controller;
use app\AdminRest;
use app\ApiRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\appoint\model\IndexProject as Pro_model;
use app\appoint\model\AdminCate;
use app\appoint\model\IndexRecord;
use app\shop\model\IndexCardCount;

class IndexProject extends ApiRest
{

    protected $model;
    protected $cate_model;
    protected $record_model;
    protected $count_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model        = new Pro_model();
        $this->cate_model   = new AdminCate();
        $this->record_model = new IndexRecord();
        $this->count_model  = new IndexCardCount();

    }
    /**
     * 预约首页
     */
    public function index(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status','=',1];
        $data['project'] = $this->model->proList($dis);
        $data['cate']    = $this->cate_model->cateSelect($dis);
        $data['config']  = Db::name('longbing_card_config')->where(['uniacid'=>$this->_uniacid])->field('id,appoint_pic,appoint_name')->find();
        $data['config']  = transImages($data['config'],['appoint_pic']);
        $data['count']   = $this->record_model->recordCount(['uniacid'=>$this->_uniacid,'user_id'=>$this->getUserId(),'status'=>1]);
        return $this->success($data);
    }

    /**
     *
     * 预约列表
     */
    public function proList(){
        $input = $this->_input;
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status','=',1];
        if(!empty($input['title'])){
            $dis[] = ['title','like',"%".$input['title']."%"];
        }
        if(!empty($input['classify_id'])){
            $dis[] = ['classify_id','=',$input['classify_id']];
        }
        $data = $this->model->proList($dis);
        return $this->success($data);
    }
    /**
     *
     * 预约详情
     */

    public function proInfo(){
        $input = $this->_param;
        $data  = $this->model->proInfo(['uniacid'=>$this->_uniacid,'id'=>$input['id'],'status'=>1]);
        if(empty($data)||empty($input['staff_id'])){
            return $this->error('not data');
        }
        if(lbIsStaff($input['staff_id'])!=1){
            return $this->error('not find card');
        }
        $data[ 'times_start' ] = explode( ',', $data[ 'times_start' ] );
        $data[ 'times_end' ]   = explode( ',', $data[ 'times_end' ] );
        $data[ 'days' ]        = explode( ',', $data[ 'days' ] );
        foreach ( $data[ 'times_start' ] as $index => $item ) {
            $data['range_time'][]=$data[ 'times_start' ][ $index ] . '-' . $data[ 'times_end' ][ $index ];
        }
        $data[ 'date' ] = array();
        $c = 0;
        while ( count( $data[ 'date' ] ) < 7 && !empty( $data[ 'days' ] ) ) {
            $count = count( $data[ 'date' ] );
            if ( $count == 7 ) {
                break;
            }
            $time = $c == 0 ? time() : strtotime( '+' . $c . ' day' );
            $w    = date( 'w', $time );
            if ( $w == 0 ) {
                $w = 7;
            }
            if ( in_array( $w, $data[ 'days' ] ) ) {
                array_push( $data[ 'date' ], date( 'Y-m-d', $time ) );
            }
            $c++;
        }
        if($this->getUserId()!=$input['staff_id']){
            $insertData = array(
                'user_id'     => $this->getUserId(),
                'to_uid'      => $input['staff_id'],
                'sign'        => 'view',
                'type'        => '17',
                'uniacid'     => $this->_uniacid,
                'target'      => $input['id']
            );
            $insert_id = $this->count_model->countAdd($insertData);
            $push_data = array(
                'action'  => 'longbingSendWxServiceNotice',
                'event'   => 'longbingSendWxServiceNotice',
                'count_id'=> $insert_id
            );
            publisher(json_encode($push_data ,true));
        }
        // $data['pro_qr'] = $this->projectQr($input['id'],$input['staff_id']);
        //By.jingshuixian  兼容新的小程序html解析工具 需要优化
        $data['content1'] = getimgsV2($data['content']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-18 15:55
     * @return void
     * descption:获取预约二维码
     */
    public function projectQr($id,$staff_id){
        $pro_qr = getCache($this->getUserId().'-'.$staff_id.'-'.$id.'-'.'app_pro_qr');
        $input['id']       = $id;
        $input['type']     = 9;
        $input['staff_id'] = $staff_id;
        $input['pid']      = $this->getUserId();
        if(empty($pro_qr)){
            $data   = longbingCreateWxCode($this->_uniacid,$input,'reserve/pages/detail');
            if(isset($data['qr_path'])) $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);
            $pro_qr = $data['qr_path'];
            setCache($this->getUserId().'-'.$staff_id.'-'.$id.'-'.'app_pro_qr',$pro_qr);
        }
        return $pro_qr;
    }










}
